
//
//  MultipleColumnsView.m
//  Doctor
//
//  Created by 阳光 on 16/10/19.
//  Copyright © 2016年 阳光. All rights reserved.
//

#import "MultipleColumnsView.h"

#define kJianXi             15
#define kClickBtnHeight     35


@interface MultipleColumnsView ()

@property (nonatomic,assign)NSInteger columns;

@end

@implementation MultipleColumnsView

-(instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame])
    {
        [self loadCreateViewLayout];
    }
    return self;
}

-(instancetype)init
{
    if ([super init])
    {
        [self loadCreateViewLayout];
    }
    return self;
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    [self loadCreateViewLayout];
}

// 设置页面布局
-(void)loadCreateViewLayout
{
    self.backgroundColor=WhiteColor;
    CGRect mainScreen=[UIScreen mainScreen].bounds;
    if (mainScreen.size.width==APPW)
    {
        self.columns=2;
    }
    else
    {
        self.columns=1;
    }
}

-(void)setDataArrayCount:(NSInteger)dataArrayCount
{
    _dataArrayCount=dataArrayCount;
    self.cellHeight=[self heightForCount:_dataArrayCount];
}

-(void)setIndexPath:(NSIndexPath *)indexPath
{
    _indexPath=indexPath;
}

-(void)setDataArray:(NSArray *)dataArray
{
    _dataArray=dataArray;
    for (UIView *view in self.subviews)
    {
        [view removeFromSuperview];
    }
    [self loadCreateMulButton];
}

// 创建UIButton
- (void)loadCreateMulButton
{
    for (NSInteger i=0;i<self.dataArray.count;i++)
    {
        
        UIButton *selectBtn=[[UIButton alloc] initWithFrame:[self frameForItemIndex:i]];

        // 是否时间已被别人预定
        BOOL isSold=[self.dataArray[i][@"order_status"] isEqualToString:@"1"]&&[self.dataArray[i][@"status"] isEqualToString:@"1"];
       
        if (isSold)
        {
            // 图片拉伸处理做背景图片
            [selectBtn setBackgroundImage:[[UIImage imageNamed:@"time_sold"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,0,0,0) resizingMode:UIImageResizingModeStretch] forState:0];
//            selectBtn.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"time_sold"]];
        }
        else
        {
            selectBtn.backgroundColor=Main_Color;
//            selectBtn.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"time_normal"]];
        }
        
        NSString *orderTimeBtnStr=[NSString stringWithFormat:@"%@--%@",[[[Util getConvertToFormalDateByTimeStamp:self.dataArray[i][@"start_time"]] componentsSeparatedByString:@" "] lastObject],[[[Util getConvertToFormalDateByTimeStamp:self.dataArray[i][@"end_time"]] componentsSeparatedByString:@" "] lastObject]];
        [selectBtn setTitle:orderTimeBtnStr forState:0];
        [selectBtn addTarget:self action:@selector(clickBtnAct:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:selectBtn];
        selectBtn.tag=i+100;
    }
}


// btn的点击响应事件
- (void)clickBtnAct:(UIButton *)sender
{
    self.ReturnClickItemIndex(self.indexPath,sender.tag-100);
}


// 计算每个UIButton的frame
-(CGRect)frameForItemIndex:(NSInteger)count
{
    
    // 每个UIButton的宽度
    CGFloat itemW=(APPW-(self.columns+1)*kJianXi)/self.columns;
    // 计算xy轴的坐标
    CGFloat x=count%self.columns*itemW +kJianXi *(count%self.columns+1);
    CGFloat y=count/self.columns*kClickBtnHeight+kJianXi*(count/self.columns+1);
    
    return CGRectMake(x,y,itemW,kClickBtnHeight);
}

// 根据数据计算高度
-(CGFloat)heightForCount:(NSInteger)count
{
    // 计算行数
    long row=count/self.columns;
    
    if (count%self.columns!=0)
    {
        row++;
    }
    
    // 每个UIButton的宽度
    CGFloat height=kClickBtnHeight*row+kJianXi*(row+1);
    
    return height;
}


@end
